package com.tg.paw.oms.controller;

import cn.hutool.core.date.DateUtil;
import com.tg.paw.oms.mq.producer.OrderSender;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * OmsController
 *
 * @author Rubble
 * @date 2020/6/12
 */
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderSender orderSender;


    @GetMapping("/lock")
    public Object demoLock(String key){
        RLock lock = redissonClient.getLock(key);
        lock.lock();
        log.info("lock key {} time: {}", key, DateUtil.now());
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.unlock();
        log.info("unlock key {} time: {}", key, DateUtil.now());
        return DateUtil.now();
    }

    @GetMapping("/orderSend")
    public Object orderSend(String key) {
        orderSender.send(key);
        return DateUtil.now();
    }
    @GetMapping("/hello")
    public Object hello() {
        return DateUtil.now();
    }
}
